package com.chijun.mform.field.impl;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.chijun.mform.R;
import com.chijun.mform.create.mode.Form;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.field.BaseField;
import com.chijun.mform.full.IGetFieldAllFormListener;
import com.chijun.mform.process.dialog.OpenProcessView;
import com.example.ccj.b.c.dialog.LoadingDialog;
import com.example.ccj.u.ActivityUtils;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.ToastUtils;
import com.github.gzuliyujiang.wheelpicker.OptionPicker;

import java.util.List;

import io.reactivex.ObservableEmitter;

/**
 * 关联表单
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/23 15:44
 */
public class FieldFor17GuanLianBiaoDan extends BaseField {
    public FieldFor17GuanLianBiaoDan(){
        super();
    }

    @Override
    public View getWritelView(Context context, WriteFormClass writeFormClass) {
        FormField formField = writeFormClass.formField;
        //提示文本
        if (formField.getHint() != null) writeFormClass.fieldFullContentView.setHint(formField.getHint());
        //输入文本颜色
        if (formField.getTextColor() != 0) writeFormClass.fieldFullContentView.setTextColor(formField.getTextColor());


        //默认文本
        writeFormClass.iHandleWriterData.getAllForm(new IGetFieldAllFormListener() {
            @Override
            public void onResult(List<Form> forms) {
                String textFieldZanCunData = getTextFieldZanCunData(context,"fieldId=?",String.valueOf(formField.getId()));
                for (Form form : forms) {
                    if (String.valueOf(form.id).equals(textFieldZanCunData)) {
                        writeFormClass.fieldFullContentView.setText(form.name);
                        saveValue = String.valueOf(form.id);
                        writeFormClass.inputIsOK = true;
                    }
                }
            }
            @Override
            public void onError(String errorStr) {
            }
        });

        writeFormClass.fieldFullContentView.setKeyListener(null);
        writeFormClass.fieldFullContentView.setFocusable(false);
        writeFormClass.fieldFullJumpImgView.setVisibility(View.VISIBLE);
        writeFormClass.fieldFullJumpImgView.setImageResource(R.drawable.field_icon_guanlianbiaodan);
        writeFormClass.fieldFullJumpImgView.setOnClickListener(v -> {
            LoadingDialog loadingDialog = new LoadingDialog(v.getContext());
            loadingDialog.showLoading();
            if (writeFormClass.iHandleWriterData != null) {
                writeFormClass.iHandleWriterData.getAllForm(new IGetFieldAllFormListener() {
                    @Override
                    public void onResult(List<Form> forms) {
                        OptionPicker optionPicker = new OptionPicker(ActivityUtils.getTopActivity());
                        optionPicker.getWheelView().setFormatter(item -> ((Form)item).name);
                        optionPicker.setData(forms);
                        optionPicker.setOnOptionPickedListener((position, item) -> {
                            writeFormClass.fieldFullContentView.setText(((Form)item).name);
                            saveValue = String.valueOf(((Form)item).id);
                        });
                        loadingDialog.dismiss();
                        optionPicker.show();
                    }

                    @Override
                    public void onError(String errorStr) {
                        loadingDialog.dismiss();
                    }
                });
            } else {
                loadingDialog.dismiss();
                ToastUtils.showShort("调用失败！");
                LogUtils.e("关联表单失败：应在调用位置，通过此方法设置回调loadFormView.getLoadFormFieldView().setiHandleWriterData，获取完毕所有表单进行展示，并将最后选定的表单通过GuanLianListener返回给字段");
            }
        });

        return writeFormClass.view;
    }


    @Override
    protected View getShowFullView(Context context, FullClass fullModel) {
        //字段名属性设置
        fullModel.setFieldProperty();

        //设置字段值
        String fieldValue = fullModel.getFieldValue();
        if (StringUtils.isNoEmpty(fieldValue)) {
            fullModel.loadContent.setText(fieldValue);
        }
        fullModel.loadContent.setKeyListener(null);
        fullModel.loadContent.setFocusable(false);
        fullModel.loadContent.setTextIsSelectable(true);//可选择

        //打开表单
        fullModel.loadContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OpenProcessView openProcessView = new OpenProcessView(fullModel.getContext(), fieldValue, fullModel.iHandlerLoadFormFull);
                openProcessView.show();
            }
        });

        return fullModel.layout;
    }


    /**
     * 字段设置界面
     * @param context 上下文
     * @param installClass 配置
     * @return 1
     */
    @Override
    protected View getFieldInstallView(Context context, InstallClass installClass) {
        ObservableEmitter<ViewGroup> emitter = installClass.emitter;

        emitter.onNext(installClass.getIsMastView());        //是否必填
        emitter.onNext(installClass.getIsPrintView());        //是否参与打印
        return installClass.v;
    }

}
